<template>
  <div class='xtx-goods-page' v-if="goods.categories">
    <div class="container">
     <!-- 面包屑 -->
     <XtxBread >
          <XtxBreadItem to="/">首页</XtxBreadItem>
          <XtxBreadItem :to="`/category/${goods.categories[1].id}`">
                      {{goods.categories[1].name}}
          </XtxBreadItem>
          <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">
              {{goods.categories[0].name}}
          </XtxBreadItem>
          <XtxBreadItem>
              {{goods.name}}
          </XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
           <!-- 图片预览区 -->
    <div class="media">
        <GoodsImage :imageList="goods.mainPictures"/>
        <GoodsSales :goods="goods"/>
    </div>
    <!-- 商品信息区 -->
    <div class="spec">
        <GoodsName :goods="goods"/>
        <!-- SKU -->
        <goodsSku :goods="goods" @change="getSku"/>
        <!-- 数量组件 -->
        <XtxNumbox v-model="number"/>
        <!-- 按钮组件 -->
        <XtxButton @click="add" type="primary" style="margin-top:20px;">加入购物车</XtxButton>
    </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
              <GoodsTab/>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
            <GoodsHot :hot-type="1" />
            <GoodsHot :hot-type="2" />
            <GoodsHot :hot-type="3" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoods } from '@/api/goods'
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import GoodsImage from './components/goods-image'
import GoodsSales from './components/goods-sales'
import GoodsName from './components/goods-name'
import goodsSku from './Sku'
import GoodsTab from './components/goods-tabs'
import GoodsHot from './components/goods-hot'
import Message from '@/components/Message/index'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsImage,
    GoodsSales,
    GoodsName,
    goodsSku,
    GoodsTab,
    GoodsHot
  },
  setup () {
    const route = useRoute()
    const goods = ref({})
    async function getGoods () {
      const res = await findGoods(route.params.id)
      console.log('商品信息数据详情', res)
      goods.value = res.data.result
    }
    onMounted(() => {
      getGoods()
    })
    const currSku = ref({})
    function getSku (sku) {
      // 无效sku返回一个空对象
      currSku.value = sku
      console.log(sku)
    }

    const number = ref(0)
    const store = useStore()
    // 加入购物车
    function add () {
      // 如果sku是无效的直接return
      if (!currSku.value.skuId) {
        return Message({ type: 'warn', text: '规格不对' })
      }
      const good = {
        id: goods.value.id,
        name: goods.value.name,
        picture: goods.value.mainPictures[0],
        skuId: currSku.value.skuId,
        price: currSku.value.oldPrice,
        nowPrice: currSku.value.price,
        attrsText: currSku.value.specsText,
        stock: currSku.value.inventory,
        selected: true,
        isEffective: true,
        count: number.value
      }
      console.log('good数据是', good)
      //   触发action函数把数据存入vuex
      store.dispatch('cart/acyncInsertCart', good)
    }
    console.log('add数据', add)
    return { goods, getSku, number, add }
  }
}
</script>

<style scoped lang='less'>
  .goods-info {
      min-height: 600px;
      background: #fff;
      display: flex;
      .media {
        width: 580px;
        height: 600px;
        padding: 30px 50px;
      }
      .spec {
        flex: 1;
        padding: 30px 30px 30px 0;
      }
    }
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
